//
//  TongZhiViewController.m
//  Cunli
//
//  Created by 湛亚 on 15/6/19.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "TongZhiViewController.h"
#import "MyDataService.h"
#import "NSString+MD5.h"
#import "TongZhi.h"
#import "TongZhiDetailViewController.h"
#import "MJRefresh.h"

@interface TongZhiViewController (){
    
    NSMutableArray *tongZhiArray;
    float screenWidth;
    int contentPage;
}

@end

@implementation TongZhiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"通知通告";
    contentPage=1;
    screenWidth=[UIScreen mainScreen].bounds.size.width;
    
    if ([self.tongZhiTableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tongZhiTableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.tongZhiTableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tongZhiTableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
    tongZhiArray=[[NSMutableArray alloc]init];
    [self getTongZhiInfoWithPage:1 andBool:YES];
    [self addRefreshAttribute];
}

- (void)addRefreshAttribute
{
    __weak typeof(self) weakSelf = self;
    
    [self.tongZhiTableView addLegendHeaderWithRefreshingBlock:^{
        [weakSelf reloadDataToNew:YES]; //刷新
    }];
    
    [self.tongZhiTableView addLegendFooterWithRefreshingBlock:^{
        [weakSelf reloadDataToNew:NO]; //加载更多
    }];
    
}

- (void)reloadDataToNew:(BOOL)new{
    
    if (new) {
        contentPage=1;
        [self getTongZhiInfoWithPage:contentPage andBool:new];
    }else{
        contentPage=contentPage+1;
        [self getTongZhiInfoWithPage:contentPage andBool:new];
    }
}

-(void)getTongZhiInfoWithPage:(int)page andBool:(BOOL)new{
    
    NSMutableDictionary *CheckParams =[[NSMutableDictionary alloc]init];
    
    [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
    [CheckParams setObject:[Config getOwnAreaids] forKey:@"areaids"];
    [CheckParams setObject:[NSString stringWithFormat:@"%d",page] forKey:@"page"];
    [CheckParams setObject:@"time_down" forKey:@"orderby"];
    [CheckParams setObject:@"10" forKey:@"pagesize"];
    [CheckParams setObject:@"5" forKey:@"type"];
    NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
    [CheckParams setObject:resultSign forKey:@"sign"];
    [MyDataService requestURL:@"convenientlist.php" httpMethod:@"GET" params:CheckParams fileDatas:nil complection:^(id result) {
        
        NSLog(@"%@",result);
        
        [self.tongZhiTableView.legendHeader endRefreshing];
        [self.tongZhiTableView.legendFooter endRefreshing];
        
        if (![[result objectForKey:@"response"] isEqualToString:@"convenientlist"]) {
            
            MBProgressHUD *hud1 = [Config createHUD];
            hud1.mode = MBProgressHUDModeCustomView;
            hud1.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error"]];
            hud1.labelText = @"获取失败!";
            [hud1 hide:YES afterDelay:1];
            return ;
        }
        
        NSMutableArray *arr=[result objectForKey:@"list"];
        if (arr.count==0) {
            
            if (!new) {
                
                MBProgressHUD *hud = [Config createHUD];
                hud.mode = MBProgressHUDModeCustomView;
                hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"done"]];
                hud.labelText = @"没有更多数据";
                [hud hide:YES afterDelay:1];
                contentPage=contentPage-1;
                [self.tongZhiTableView.legendFooter setState:MJRefreshFooterStateNoMoreData];
            }
            return ;
        }
        
        if (new) {
            [tongZhiArray removeAllObjects];
        }
        for (NSDictionary *dic in arr) {
            //TongZhi *tongZhi=[[TongZhi alloc]initWithDictionary:dic];
            TongZhi *tongZhi=[TongZhi yy_modelWithDictionary:dic];
            [tongZhiArray addObject:tongZhi];
        }
        [self.tongZhiTableView reloadData];
        
    }failureAction:^{
        
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return tongZhiArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdentifier=@"cell";
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    if(!cell){
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    TongZhi *tongZhi=[tongZhiArray objectAtIndex:indexPath.row];
    
    UILabel *titleLab=[[UILabel alloc]initWithFrame:CGRectMake(60, 10, screenWidth-70, 22)];
    titleLab.numberOfLines=0;
    titleLab.text=tongZhi.title;
    [cell addSubview:titleLab];
    
    UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 43, 22)];
    
    if (tongZhi.type==1) {
        img.image=[UIImage imageNamed:@"tongzhi.png"];
    }else{
        img.image=[UIImage imageNamed:@"tonggao.png"];
    }
    [cell addSubview:img];
    
    UILabel *timeLab=[[UILabel alloc]initWithFrame:CGRectMake(screenWidth-160, 45, 150, 10)];
    timeLab.text=tongZhi.addtime;
    timeLab.textAlignment=2;
    timeLab.font=[UIFont systemFontOfSize:13];
    [cell addSubview:timeLab];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    TongZhi *tongzhi=[tongZhiArray objectAtIndex:indexPath.row];
    TongZhiDetailViewController *tongZhiVC=[[TongZhiDetailViewController alloc]init];
    tongZhiVC.itemid=tongzhi.itemid;
    tongZhiVC.type=tongzhi.type;
    tongZhiVC.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:tongZhiVC animated:YES];
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
